<?php

namespace App\Http\Controllers\Tenement;

use App\Common\json;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class BankController extends Controller{

    //获取全部银行卡号
    public function AllBank(Request $request){
        if ($request ->isMethod('post')){
            $user_bank        = $request ->input('village_id');
            $data             = DB::table('bank')
                ->select('bank_id','bankName','bankCardno')
                ->where('user_bank',$user_bank)
                ->get();
            foreach ($data as $value){
                $bankname      = $value ->bankName;
                $bankcardno    = $value ->bankCardno;
                $da[]          = array('bank_id'=>$value->bank_id,'bankname'=>$bankname,'bankcardno'=>$bankcardno);
            }
            if($data == true){
                return json::json('success', '银行卡号获取成功', $da);
            }else{
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }
        }
    }

    //绑定
    public function DindingBank(Request $request){
        if($request ->isMethod('post')){
            $Array            = (object)array();
            $user_bank        = $request ->input('village_id');
            $bank_id          = $request ->input('bank_id');
            $update           = DB::table('bank')
                ->where('user_bank',$user_bank)
                ->where('bank_id',$bank_id)
                ->update([
                    'removed'  =>1
                ]);
            if($update == true){
                DB::table('bank')->where('user_bank',$user_bank)
                    ->where('bank_id','!=',$bank_id)
                    ->update([
                        'removed' =>0
                    ]);
                return json::json('success', '银行卡号绑定成功', $Array);
            }else{
                return json::json('failure', '由于网络或者不正当原因失败', $Array);
            }

        }
    }

    //添加银行卡号
    public function AddBank(Request $request){
        if($request ->isMethod('post')){
            $Array            = (object)array();
            $user_bank        = $request ->input('village_id');
            $bankname         = $request ->input('bankName');
            $bankcardno       = $request ->input('bankcardno');

            $bank_create_time = date('Y-m-d H:i:s');
            $select           = DB::table('bank')
                ->where('user_bank',$user_bank)
                ->where('bankName',$bankname)
                ->where('bankCardNo',$bankcardno)
                ->get();
            if($select == true){
                return json::json('failure', '添加的银行卡号存在数据库中', $Array);
            }else{
                $data         = DB::table('bank')
                    ->insert([
                        'user_bank'   =>$user_bank,
                        'bankName'    =>$bankname,
                        'bankCardNo'  =>$bankcardno,
                        'bank_create_time' =>$bank_create_time
                    ]);
                if($data == true){
                    return json::json('success', '银行卡号添加成功', $Array);
                }else{
                    return json::json('failure', '由于网络或者不正当原因失败', $Array);
                }
            }
        }
    }

    //更新银行信息
    public function UpdateBank(Request $request){
        if($request ->isMethod('post')){
            $Array            = (object)array();
            $bank_id          = $request ->input('bank_id');
            $bankname         = $request ->input('bankname');
            $bankcardno       = $request ->input('bankcardno');
            $bank_update_time = date('Y-m-d H:i:s');
            $data             = DB::table('bank')
                ->where('bank_id',$bank_id)
                ->update([
                    'bankName'    =>$bankname,
                    'bankCardNo'  =>$bankcardno,
                    'bank_update_time' =>$bank_update_time
                ]);
            if($data == true){
                return json::json('success', '银行卡号修改成功', $Array);
            }else{
                return json::json('failure', '由于网络或者不正当原因失败', $Array);
            }
        }
    }

    //删除银行信息
    public function DelBank(Request $request){
        if($request ->isMethod('post')){
            $Array            = (object)array();
            $bank_id          = $request ->input('bank_id');
            $data             = DB::table('bank')
                ->where('bank_id',$bank_id)
                ->delete();
            if($data == true){
                return json::json('success', '银行卡号删除成功', $Array);
            }else{
                return json::json('failure', '由于网络或者不正当原因失败', $Array);
            }
        }
    }


    //银行列表与银行代码列表
    public function SelectBank(Request $request){
        if($request ->isMethod('get')){
            $data       = DB::table('banklist')
                ->select('banklist_name','banklist_bankcode')
                ->get();
            foreach ($data as $value){
                $name       = $value ->banklist_name;
                $bankcode   = $value ->banklist_bankcode;
                $da[]         = array('banklist_name' =>$name,'banklist_bankcode'=>$bankcode);
            }
            if($data == true){
                return json::json('success', '获取银行列表与代码列表', $da);
            }else{
                return json::json('failure', '由于网络或者不正当原因失败', '');
            }
        }
    }

    //收入明细

    public function detailsum($type,$village_id){
        switch ($type){
            case "1":
                $data   = DB::table('order')
                    ->select('order_type','sum','order_create_time')
                    ->leftjoin('user','user.user_id','=','order.user_order')
                    ->where('user_village',$village_id)
                    ->where('order_type',2)
                    ->orderby('order_create_time','ase')
                    ->get();
                foreach ($data as $value){
                    $dd[]  = array(
                        'order_type' =>$value ->order_type,
                        'sum'         =>$value ->sum,
                        'order_create_time' =>$value ->order_create_time
                    );
                }
                if(empty($dd)){
                    return array();
                }else {
                    return $dd;
                }
            case "2":
                $data   = DB::table('order')
                    ->select('order_type','sum','order_create_time')
                    ->leftjoin('user','user.user_id','=','order.user_order')
                    ->where('user_village',$village_id)
                    ->where('order_type',3)
                    ->orderby('order_create_time','ase')
                    ->get();
                foreach ($data as $value){
                    $dd[]  = array(
                        'order_type' =>$value ->order_type,
                        'sum'         =>$value ->sum,
                        'order_create_time' =>$value ->order_create_time
                    );
                }
                if(empty($dd)){
                    return array();
                }else {
                    return $dd;
                }
            case "3":
                $data   = DB::table('withdrawal')
                    ->where('withdrawal_village',$village_id)
                    ->orderby('withdrawal_create_time','ase')
                    ->get();
                foreach ($data as $value){
                    $dd[]  = array(
                        'order_type' =>"4",
                        'sum'         =>$value ->withdrawal_money,
                        'order_create_time' =>$value ->withdrawal_create_time
                    );
                }
                if(empty($dd)){
                    return array();
                }else {
                    return $dd;
                }
        }
    }


    public function detail(Request $request){
        if($request ->isMethod('post')){
            $village_id     = $request ->input('village_id');
            $type_one       = $this->detailsum(1,$village_id);
            $type_two       = $this->detailsum(2,$village_id);
            $type_three     = $this ->detailsum(3,$village_id);

            $data  = array_merge($type_one,$type_two,$type_three);
            foreach ($data as $key => $row)
            {
                $volume[$key]  = $row['order_type'];
                $edition[$key] = $row['sum'];
                $time[$key]    = $row['order_create_time'];
            }
            array_multisort($volume, SORT_DESC, $time, SORT_ASC, $data);

            if($data == true){
                return json::json('success', '获取明细信息成功', $data);
            }else{
                return json::json('failure', '对不起没有找到你要的明细信息', []);
            }
        }
    }

    //提现
    public function withdraw(Request $request){
        if($request ->isMethod('post')){
            $arr = (object)array();
            $village_id      = $request ->input('village_id');
            $withdrawal_number  = date('Ymdhis');
            $withdrawal_money   = $request ->input('money'); //金额
            $work_id            = $request ->input('work_id');
            $withdrawal_bank    = $request ->input('bank_id');
            $withdrawal_create_time = date('Y-m-d H:i:s');
            $select                  = DB::table('village')->where('village_id',$village_id)->first();
            if($withdrawal_money>$select->village_wallet){
                return json::json('failure', '对不起提取金额不足', $arr);
            }else {
                $data = DB::table('withdrawal')->insert([
                    'withdrawal_number' => $withdrawal_number,
                    'withdrawal_village' => $village_id,
                    'withdrawal_money' => $withdrawal_money,
                    'withdrawal_work' => $work_id,
                    'withdrawal_bank' => $withdrawal_bank,
                    'withdrawal_create_time' => $withdrawal_create_time
                ]);

                if ($data == true) {
                    $money  = $select->village_wallet - $withdrawal_money;
                    DB::table('village')->where('village_id',$village_id)->update(['village_wallet'=>$money]);
                    return json::json('success', '提取现金成功', $arr);
                } else {
                    return json::json('failure', '对不起提取失败', $arr);
                }
            }
        }
    }

    //金额
    public function villagemoney(Request $request){
        if($request ->isMethod('post')){
            $village_id    = $request ->input('village_id');
            $data          = DB::table('village') ->where('village_id',$village_id)->first();
            if($data ->village_wallet ==null){
                $value   = array('wallet'=>"");
            }else{
                $value  = array('wallet'=>$data->village_wallet);
            }
            $arr   = (object)array();
            if($data == true){

                return json::json('success', '获金额成功', $value);
            }else{
                return json::json('failure', '对不起获金额失败', $arr);
            }

        }
    }


}